<?php

namespace fast;

use think\Image as thinkImage;
use think\image\Exception as ImageException;

class Image extends thinkImage
{

    /**
     * 显示自定义尺寸缩略图
     *
     * @param int $w
     * @param int $h
     */
    public function showImage($w = 200, $h = 200)
    {
        if ($w > 600) $w = 600;
        if ($h > 800) $h = 800;
        $this->thumb($w, $h);
        Header("Content-type: image/png");
        ob_start(); //打开缓冲区
        imagepng($this->im, null, 9);
        ob_end_flush();//输出全部内容到浏览器
        die;
    }

    /**
     * 下载自定义尺寸图片
     *
     * @param int $w
     * @param int $h
     */
    public function downloadImage($w = 200, $h = 200)
    {
        $this->thumb($w, $h);
        header("Content-type: octet/stream");
        header("Content-disposition:attachment;filename=image.png;");
        ob_start(); //打开缓冲区
        imagepng($this->im, null, 9);
        ob_end_flush();//输出全部内容到浏览器
        die;
    }

    /**
     * 根据路径加载图片文件
     *
     * @param \SplFileInfo|string $file
     * @return Image
     */
    public static function open($file)
    {
        if (is_string($file)) {
            $file = new \SplFileInfo($file);
        }
        if (!$file->isFile()) {
            //throw new ImageException('image file not exist');
            Header("Content-type: image/png");
            die;
        }
        return new self($file);
    }


}